package com.girant.logger.controller.view;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.girant.excel.utils.ExcelUtil;
import com.girant.logger.entity.dto.ErrorLogDTO;
import com.girant.logger.entity.dto.ErrorLogExportDTO;
import com.girant.logger.entity.vo.form.ErrorLogFormExportVO;
import com.girant.logger.entity.vo.form.ErrorLogPageFormVO;
import com.girant.logger.entity.vo.show.ErrorLogExportVO;
import com.girant.logger.entity.vo.show.ErrorLogVO;
import com.girant.model.utils.Result;
import com.girant.mybatis.entity.BasePage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import com.girant.logger.service.ErrorLogService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.girant.logger.service.ErrorlogExportService;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Tag(name = "异常日志接口")
@RestController
@RequestMapping("/log/error")
public class ErrorLogController {
    @Resource
    private ErrorLogService errorLogService;
    @Resource
    private ErrorlogExportService ErrorlogExportService;

    //异常日志分页查询
    @Operation(summary = "异常日志分页列表", description = "根据传入的查询条件（如时间范围、服务名称、错误级别等），分页返回错误日志列表")
    @PostMapping("/loggedPage")
   // @CheckPermission("sys:log:query:info")
    public Result<BasePage<ErrorLogVO>> loggedPage(@Validated ErrorLogPageFormVO request) {

        //转换类型成DTO
        ErrorLogDTO requestDTO = BeanUtil.copyProperties(request, ErrorLogDTO.class);

        // 调用 service 查询分页数据
        BasePage<ErrorLogVO> pageData = errorLogService.queryErrorLogPage(requestDTO);

        return Result.success(pageData);
    }

    @Operation(summary = "错误日志列表Excel导出", description = "根据传入的查询条件（如时间范围、服务名称、错误级别等），导出Excel")
    @PostMapping("/loggedExport")
   // @CheckPermission("sys:log:query:info")
    public void businessLogsExport(@Validated ErrorLogFormExportVO request, HttpServletResponse response) throws IOException {
            //1.类型转换
        ErrorLogExportDTO requestDTO = BeanUtil.copyProperties(request, ErrorLogExportDTO.class);
        if(ObjectUtil.isNotEmpty(request.getOperationStartTimeLd())){
            requestDTO.setOperationStartTime(request.getOperationStartTimeLd());
        }

            // 2.查询导出数据
            List<ErrorLogExportVO> excelResource = ErrorlogExportService.exportLog(requestDTO);

            // 3.生成符合业务的文件名（修正文件名）
            String fileName = generateDocExportFileName("异常日志导出");

            // 4.输出Excel到响应流（确保工具类正确处理response）
            ExcelUtil.write(ErrorLogExportVO.class, response, fileName, excelResource);
    }

    public static String generateDocExportFileName(String docName) {
        return docName + DateUtil.format(new Date(), "yyyy-MM-dd");
    }
}

